package com.brainhealth.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具
 */
public class RegexUtil {

    private static final String DIGITAL = "^[0-9]*$";

    private static final String REGEX_ID_CARD = "(^\\d{15}$)|(^\\d{17}([0-9]|X)$)";

    private static final String YYYY_MM_DD = "^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$";

    /**
     * 日期格式字符串（yyyy-MM-DD）校验
     * @param phone
     * @return
     */
    public static boolean isValidPhone(String phone) {
        if (!Pattern.matches(DIGITAL, phone)){
            return false;
        }
        if (phone.length()>11){
            return false;
        }
        return true;
    }

    /**
     * 日期格式字符串（yyyy-MM-DD）校验
     * @param date
     * @return
     */
    public static boolean isValidDate(String date) {
        return Pattern.matches(YYYY_MM_DD, date);
    }

    /**
     * 身份证号校验
     * @param idCard
     * @return
     */
    public static boolean isValidIdCard(String idCard) {
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }

    public static boolean isRightChar(char c) {
        return isChinese(c) || isWord(c);
    }

    /**
     * 昵称校验（10字符内中英文）
     * @param nickName
     * @return
     */
    public static boolean isValidNickName(String nickName){
        if (StringUtils.isEmpty(nickName)){
            return false;
        }
        char[] chars = nickName.toCharArray();
        Integer length=0;
        for (char aChar : chars) {
            boolean chineseFlag = isChinese(aChar);
            boolean wordFlag = isWord(aChar);
            if (!chineseFlag&&!wordFlag){
                return false;
            }
            if (chineseFlag){
                length+=2;
            }
            if (wordFlag){
                length+=1;
            }
            if (length>10){
                return false;
            }
        }
        return true;
    }


    /**
     * 校验某个字符是否是a-z、A-Z、_、0-9
     *
     * @param c 被校验的字符
     * @return true 代表符合条件
     */
    public static boolean isWord(char c) {
        String regEx = "[a-zA-Z]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher("" + c);
        return m.matches();
    }

    /**
     * 判定输入的是否是汉字
     *
     * @param c 被校验的字符
     * @return true 代表是汉字
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}
